package com.rockwell.robot2015;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import com.rockwell.robot2015.views.MazeCameraView;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ColorActivity extends Activity {
	private static final String TAG = "ColorActivity";
	
	private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
		@Override
		public void onManagerConnected(int status) {
			switch (status) {
			case LoaderCallbackInterface.SUCCESS: 
				setupButtons();
				break;

			default: 
				super.onManagerConnected(status);
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_color);

		if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this,
				mOpenCVCallBack)) {
			Log.e(TAG, "Cannot connect to OpenCV Manager");
		} else {
			Log.i(TAG, "opencv successfull");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.color, menu);
		return true;
	}

	private void setupButtons() {
		Button bn = (Button) findViewById(R.id.button_color_treasure);
		Scalar hsv = SettingsActivity.getTreasureColor(this);
		setBackgroundColor(bn, hsv);
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseColor(MazeCameraView.TeachMode.Treasure);
			}
			
		});
		
		TextView lbl = (TextView)findViewById(R.id.label_hsv_treasure);
		showHsvValue(lbl, hsv);
		
		bn = (Button) findViewById(R.id.button_color_maze1);
		hsv = SettingsActivity.getMaze1Color(this);
		setBackgroundColor(bn, hsv);
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseColor(MazeCameraView.TeachMode.Maze1);
			}
			
		});
		
		lbl = (TextView)findViewById(R.id.label_hsv_maze1);
		showHsvValue(lbl, hsv);
		

		bn = (Button) findViewById(R.id.button_color_maze2);
		hsv = SettingsActivity.getMaze2Color(this);
		setBackgroundColor(bn, hsv);
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseColor(MazeCameraView.TeachMode.Maze2);
			}
			
		});
		
		lbl = (TextView)findViewById(R.id.label_hsv_maze2);
		showHsvValue(lbl, hsv);
		

		bn = (Button) findViewById(R.id.button_color_maze3);
		hsv = SettingsActivity.getMaze3Color(this);
		setBackgroundColor(bn, hsv);
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseColor(MazeCameraView.TeachMode.Maze3);
			}
			
		});
		
		lbl = (TextView)findViewById(R.id.label_hsv_maze3);
		showHsvValue(lbl, hsv);
		
		/*
		bn = (Button) findViewById(R.id.button_color_obstacle);
		hsv = SettingsActivity.getObstacleColor(this);
		setBackgroundColor(bn, hsv);
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseColor(MazeCameraView.TeachMode.Obstacle);
			}
			
		});
		
		lbl = (TextView)findViewById(R.id.label_hsv_obstacle);
		showHsvValue(lbl, hsv);
		*/
		

		bn = (Button) findViewById(R.id.button_color_entrance);
		hsv = SettingsActivity.getMazeEntranceGuideColor(this);
		setBackgroundColor(bn, hsv);
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseColor(MazeCameraView.TeachMode.MazeEntranceGuide);
			}
			
		});
		
		lbl = (TextView)findViewById(R.id.label_hsv_entrance);
		showHsvValue(lbl, hsv);
		

		bn = (Button) findViewById(R.id.button_color_droparea);
		hsv = SettingsActivity.getDropAreaColor(this);
		setBackgroundColor(bn, hsv);
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseColor(MazeCameraView.TeachMode.DropArea);
			}
			
		});
		
		lbl = (TextView)findViewById(R.id.label_hsv_droparea);
		showHsvValue(lbl, hsv);
		

		bn = (Button) findViewById(R.id.button_color_outerwall);
		hsv = SettingsActivity.getOuterWallColor(this);
		setBackgroundColor(bn, hsv);
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				chooseColor(MazeCameraView.TeachMode.OuterWall);
			}
			
		});

		lbl = (TextView)findViewById(R.id.label_hsv_outerwall);
		showHsvValue(lbl, hsv);
	}

	private void refreshColors() {
		Button bn = (Button) findViewById(R.id.button_color_treasure);
		Scalar hsv = SettingsActivity.getTreasureColor(this);
		setBackgroundColor(bn, hsv);
		
		TextView lbl = (TextView)findViewById(R.id.label_hsv_treasure);
		showHsvValue(lbl, hsv);
		
		bn = (Button) findViewById(R.id.button_color_maze1);
		hsv = SettingsActivity.getMaze1Color(this);
		setBackgroundColor(bn, hsv);

		lbl = (TextView)findViewById(R.id.label_hsv_maze1);
		showHsvValue(lbl, hsv);
		
		bn = (Button) findViewById(R.id.button_color_maze2);
		hsv = SettingsActivity.getMaze2Color(this);
		setBackgroundColor(bn, hsv);

		lbl = (TextView)findViewById(R.id.label_hsv_maze2);
		showHsvValue(lbl, hsv);
		
		bn = (Button) findViewById(R.id.button_color_maze3);
		hsv = SettingsActivity.getMaze3Color(this);
		setBackgroundColor(bn, hsv);

		lbl = (TextView)findViewById(R.id.label_hsv_maze3);
		showHsvValue(lbl, hsv);
		
		/*
		bn = (Button) findViewById(R.id.button_color_obstacle);
		hsv = SettingsActivity.getObstacleColor(this);
		setBackgroundColor(bn, hsv);

		lbl = (TextView)findViewById(R.id.label_hsv_obstacle);
		showHsvValue(lbl, hsv);
		*/
		
		bn = (Button) findViewById(R.id.button_color_entrance);
		hsv = SettingsActivity.getMazeEntranceGuideColor(this);
		setBackgroundColor(bn, hsv);

		lbl = (TextView)findViewById(R.id.label_hsv_entrance);
		showHsvValue(lbl, hsv);
		
		bn = (Button) findViewById(R.id.button_color_droparea);
		hsv = SettingsActivity.getDropAreaColor(this);
		setBackgroundColor(bn, hsv);

		lbl = (TextView)findViewById(R.id.label_hsv_droparea);
		showHsvValue(lbl, hsv);
		
		bn = (Button) findViewById(R.id.button_color_outerwall);
		hsv = SettingsActivity.getOuterWallColor(this);
		setBackgroundColor(bn, hsv);

		lbl = (TextView)findViewById(R.id.label_hsv_outerwall);
		showHsvValue(lbl, hsv);
	}

	private void chooseColor(MazeCameraView.TeachMode color) {
		Intent intent = new Intent(this, ChooseColorActivity.class);
		intent.putExtra(ChooseColorActivity.TEACH_MODE, color.ordinal());
		
		startActivityForResult(intent, 1);;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		refreshColors();
	} 

	private void setBackgroundColor(Button bn, Scalar hsv) {
		int rgba = convertHsv2Rgba(hsv);
		bn.setBackgroundColor(rgba);
	}

	private int convertHsv2Rgba(Scalar hsvColor) {
		Mat pointMatRgba = new Mat();
		Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor);
		Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);

		double[] color = pointMatRgba.get(0, 0);
		int r = (int) color[0];
		int g = (int) color[1];
		int b = (int) color[2];
		int a = (int) color[3];
		return (a << 24) | (r << 16) | (g << 8) | b;
	}
	
	private void showHsvValue(TextView lbl, Scalar hsv) {
		String text = String.format("%d, %d, %d", 
				(int)hsv.val[0], (int)hsv.val[1], (int)hsv.val[2]);
		
		lbl.setText(text);
	}
}
